// 封装随机函数
function random(max,min){
    return Math.floor(Math.random()*(max-min+1)+min)
}

// 封装随机颜色
function randomColor(){
    var colorArr=[0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F']
    var min=0
    var max=colorArr.length-1
    var one=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var two=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var three=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var four=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var five=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var six=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    // console.log('#'+one+two+three+four+five+six);
    return '#'+one+two+three+four+five+six
}


// 1.设置间隔定时器
setInterval(function(){
    // 1.1造div
    divObj=document.createElement('div')
    divObj.className='font'
    // 1.2造数组
    var arr=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    // 1.3设置字母随机出现
    var max=arr.length-1
    var min=0
    // 1.4放到div中
    divObj.innerText=arr[random(arr.length-1,0)]
    // 1.5设置字母出现的范围
    var maxLeft=window.innerWidth||document.documentElement.clientWidth
    var maxTop=window.innerHeight||document.documentElement.clientHeight
    var leftObj=random(maxLeft,0)
    var topObj=random(maxTop,0)
    
    divObj.style.position='absolute'
    divObj.style.left=leftObj+'px'
    divObj.style.top=topObj+'px'
    divObj.style.fontSize='40px'
    divObj.style.color=randomColor()
    // 1.6追加到网页中
    document.body.appendChild(divObj)
   
},3000)

// 二.清除字母
// 2.给网页绑定鼠标按下事件
document.body.onkeyup=function(evt){
    // 2.1获取事件对象
    var e=evt||window.event
console.log(e.key)  //按下的是字母
var fonts=document.querySelectorAll('div.font')
console.log(fonts);
fonts.forEach(function(item,i){   //item是每个div
    if(item.innerText==e.key){
        document.body.removeChild(item)
    }

})


}